package io.renren.modules.stock.controller;

import cn.hutool.poi.excel.ExcelReader;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.stock.entity.ConceptEntity;
import io.renren.modules.stock.entity.LimitUpEntity;
import io.renren.modules.stock.service.ConceptService;
import io.renren.modules.stock.service.LimitUpService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;


/**
 * 涨停板
 *
 * @author chenshun
 * @email 979944697@qq.com
 * @date 2022-02-27 13:46:35
 */
@RestController
@RequestMapping("stock/limitup")
public class LimitUpController {
    @Autowired
    private LimitUpService limitUpService;

    @Autowired
    private ConceptService conceptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("stock:limitup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = limitUpService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("stock:limitup:info")
    public R info(@PathVariable("id") Long id){
		LimitUpEntity limitUp = limitUpService.getById(id);

        return R.ok().put("limitUp", limitUp);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("stock:limitup:save")
    public R save(@RequestBody LimitUpEntity limitUp){
		limitUpService.save(limitUp);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("stock:limitup:update")
    public R update(@RequestBody LimitUpEntity limitUp){
		limitUpService.updateById(limitUp);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("stock:limitup:delete")
    public R delete(@RequestBody Long[] ids){
		limitUpService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/folder/save")
//    @RequiresPermissions("stock:limitup:save")
    public R save(@RequestParam String folder) throws IOException {
        String parentDir = "F:\\情绪数据" + File.separator + folder;
        List<LimitUpEntity> limitUpEntities = new ArrayList<>();
        List<ConceptEntity> conceptEntities = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        try (Stream<Path> walk = Files.walk(Paths.get(parentDir))) {
            System.out.println("完成读取资源");
            walk.forEach((path) -> {
                File bookFile = path.toFile();
                if (!bookFile.isDirectory()) {
                    ExcelReader excelReader = new ExcelReader(bookFile, 0);
                    List<List<Object>> content = excelReader.read();
                    String name = bookFile.getName();
                    if (name.contains("saved")) {
                        return;
                    }
                    if (name.contains("zt")) {
                        for (int i = 1; i < content.size(); i++) {
                            LimitUpEntity limitUpEntity = new LimitUpEntity();
                            List<Object> objects = content.get(i);
                            limitUpEntity.setSymbol((String) objects.get(0));
                            limitUpEntity.setStockChiName((String) objects.get(1));
                            limitUpEntity.setLimitUpDays(Integer.parseInt((String) objects.get(2)));
                            limitUpEntity.setConcept((String) objects.get(3));
                            limitUpEntity.setPrice(Double.parseDouble((String) objects.get(4)));
                            limitUpEntity.setAmountOfIncrease(Double.parseDouble((String) objects.get(5)));
                            limitUpEntity.setBuyLockVolumeRatio(Double.parseDouble((String) objects.get(6)));
                            limitUpEntity.setExchangeRatio(Double.parseDouble((String) objects.get(7)));
                            limitUpEntity.setCirculationMarketValue(Double.parseDouble((String) objects.get(8)));
                            limitUpEntity.setTotalValue(Double.parseDouble((String) objects.get(9)));
                            try {
                                limitUpEntity.setFirstLimitUp(simpleDateFormat1.parse((String) objects.get(10)));
                                limitUpEntity.setLastLimitUp(simpleDateFormat1.parse((String) objects.get(11)));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            limitUpEntity.setBreakLimitUpTimes(Integer.parseInt((String) objects.get(12)));
                            limitUpEntity.setContinuation((String) objects.get(13));
                            limitUpEntity.setSurgeReason((String) objects.get(14));
                            limitUpEntity.setHotTopic((String) objects.get(15));
                            StringJoiner stringJoiner = new StringJoiner("-");
                            stringJoiner.add(name.substring(2, 6));
                            stringJoiner.add(name.substring(6, 8));
                            stringJoiner.add(name.substring(8, 10));
                            String dateStr = stringJoiner + " 15:00:00";

                            try {
                                limitUpEntity.setDate(simpleDateFormat.parse(dateStr));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            limitUpEntities.add(limitUpEntity);
                        }
                    } else {
                        for (int i = 1; i < content.size(); i++) {
                            List<Object> objects = content.get(i);
                            ConceptEntity conceptEntity = new ConceptEntity();
                            conceptEntity.setConcept((String) objects.get(0));
                            conceptEntity.setLimitUpStockNumber(Integer.parseInt((String) objects.get(1)));
                            conceptEntity.setHotTopic((String) objects.get(2));
    //                        yyyy-MM-dd HH:mm:ss
                            StringJoiner stringJoiner = new StringJoiner("-");
                            stringJoiner.add(name.substring(0, 4));
                            stringJoiner.add(name.substring(4, 6));
                            stringJoiner.add(name.substring(6, 8));
                            String dateStr = stringJoiner + " 15:00:00";

                            try {
                                conceptEntity.setDate(simpleDateFormat.parse(dateStr));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            conceptEntities.add(conceptEntity);
                        }
                    }
                    File file =
                        new File(parentDir + File.separator + name.replace(".xlsx", "saved.xlsx"));
                    bookFile.renameTo(file);
                    System.out.println("完成读取一个文件" + name);
                }
            });
        }
        limitUpService.saveBatch(limitUpEntities);
        conceptService.saveBatch(conceptEntities);
        System.out.println("保存完！");
        return R.ok();
    }
}
